package controller.Seller;

import pojo.Seller;
import pojo.Superuser;
import service.Seller.SellerService;
import service.Seller.impl.SellerServiceImpl;
import service.Supper.SuperuserService;
import service.Supper.impl.SuperuserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * 验证用户登录
 */
@WebServlet(name = "SellerServlet", urlPatterns = "/doSelServlet")
public class SellerServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        SellerService sls = new SellerServiceImpl();
        String action = request.getParameter("action");
        if (action == null || action == "") {
            action = "cusLogin";
        }
        if ("cusLogin".equals(action)) {    //验证用户名和密码
            String userLoginName = request.getParameter("userLoginName");
            String userPassword = request.getParameter("userPassword");
            //验证用户名密码是否正确
            Seller seller = sls.CheckSuser(userLoginName, userPassword);
            boolean flag = false;
            if (seller.getSellerUser() != null && seller.getSellerPassword() != null) {
                flag = true;
                request.getSession().setAttribute("Sel", seller);
            } else {
                flag = false;
            }
            String result = "{\"flag\":\"" + flag + "\"}";
            out.write(result);
        } else if ("selQueryById".equals(action)) {   //根据id查询
            Long id = Long.valueOf(request.getParameter("id"));
            Seller seller = sls.SelSuser(id);
            request.getSession().setAttribute("Sel", seller);
            response.sendRedirect(request.getContextPath() + "/Seller/sellerUpdate.jsp");
        } else if ("selUpdateLogin".equals(action)) {   //修改个人信息
            //得到session中的数据并进行修改
            Seller sel = (Seller) request.getSession().getAttribute("Sel");
            sel.setId(Long.valueOf(request.getParameter("id")));
            sel.setSellerName(request.getParameter("sellerName"));
            sel.setSellerUser(request.getParameter("sellerUser"));
            sel.setSellerSex(request.getParameter("sellerSex"));
        //    sel.setSellerBirthday(Timestamp.valueOf(request.getParameter("sellerBirthday")));
            try {
                sel.setSellerBirthday(sdf.parse(request.getParameter("sellerBirthday")));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            sel.setSellerIdCard(request.getParameter("sellerIdCard"));
            sel.setSellerEmail(request.getParameter("sellerEmail"));
            sel.setSellerTel(request.getParameter("sellerTel"));
            sel.setSellerAddress(request.getParameter("sellerAddress"));
            int n = sls.updateSuser(sel);
            if (n > 0) {
                //再次进行存储
                request.getSession().setAttribute("Sel", sel);
                out.print("<script>");
                out.print("alert('修改成功');");
                out.print("location.href='/Seller/new.jsp';");
                out.print("</script>");
            } else {
                out.print("<script>");
                out.print("alert('修改失败');");
                out.print("location.href='/Seller/sellerUpdate.jsp';");
                out.print("</script>");
            }
        }else if("QueryById".equals(action)){   //进入修改密码
            Long id = Long.valueOf(request.getParameter("id"));
            Seller seller = sls.SelSuser(id);
            request.getSession().setAttribute("Sel", seller);
            response.sendRedirect(request.getContextPath() + "/Seller/sellerUpdatePwd.jsp");
        }else if("sellerUpdatePwd".equals(action)){   //执行修改密码方法
            //得到session中的数据并进行修改
            Seller sel = (Seller) request.getSession().getAttribute("Sel");
            sel.setSellerPassword(request.getParameter("sellerPassword"));
            int n = sls.updateSuserPwd(sel);
            if(n>0){
                request.getSession().setAttribute("Sel",sel);
                out.print("<script>");
                out.print("alert('修改密码成功');");
                out.print("location.href='/Seller/new.jsp';");
                out.print("</script>");
            }else{
                out.print("<script>");
                out.print("alert('修改密码失败');");
                out.print("location.href='/Seller/sellerUpdatePwd.jsp';");
                out.print("</script>");
            }
        }else if("out".equals(action)){    //注销
            request.getSession().invalidate();
            response.sendRedirect(request.getContextPath()+"/Login.jsp");
        }
        out.flush();
        out.close();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
